//
//  LoginViewController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class LoginViewController: BaseViewController {
    
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var lineView1: UIView!
    @IBOutlet weak var lineView2: UIView!
    @IBOutlet weak var loginBtn: UIButton!
    @IBOutlet weak var topTexField: UITextField!
    @IBOutlet weak var bottomTexField: UITextField!
    @IBOutlet weak var regesterBtn: UIButton!
    @IBOutlet weak var findPasswordBtn: UIButton!
    @IBOutlet weak var leftLineView: UIView!
    @IBOutlet weak var rightLineView: UIView!
    @IBOutlet weak var bottomFirstLabel: UILabel!
    @IBOutlet weak var bottomSendLabel: UILabel!
    @IBOutlet weak var bottomThirdLabel: UILabel!
    
    

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: true)
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        settingSubViews()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
   @IBAction func cancelBtnDidTap(btn : UIButton){
    
    dismiss(animated: true, completion: nil)
    
    }
    
    
    @IBAction func loginBtnDidTap(button : UIButton){
        
        view.endEditing(true)
        
        SVProgressHUD.show(withStatus: "正在登录...")
        
        
        var parameters  = [String : AnyObject]()
        
        parameters["username"] = topTexField.text as AnyObject
        
        parameters["password"] = bottomTexField.text as AnyObject
        
        NetworkTools.shared.sendRequest(method: .POST_METHOD, pathStr: "mobile/index.php?c=login&a=index", parameters: parameters ) { (responseObject, error) in
            
            if error == nil {
                
             SVProgressHUD.showSuccess(withStatus: "登录成功!")
                
                let user = UserInfo(dic: responseObject!["datas"] as! [String : AnyObject])
                
                UserViewModel.shared.user = user
                
                user.saveUserInfo()
                
             self.dismiss(animated: true, completion: nil)


                
            }else{
                
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
        
    }
    
    
    @IBAction func regsterBtnDidTap(button : UIButton){
        
        QLPlusLine()
    }
    
    @IBAction func findPasswordBtnDidTap(button : UIButton){
        
        QLPlusLine()
        
    }
    
    
    
    deinit {
        SVProgressHUD.dismiss()
    }


}



extension LoginViewController{

    fileprivate func settingSubViews(){
        
        
       cancelBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        
        topTexField.settingPlaceholder(text: "请输入账号", color: UIColor.placeholderColor(), font:  UIFont.systemFont(ofSize: 17))
        
        topTexField.tintColor = UIColor.themeColor()
        
        bottomTexField.settingPlaceholder(text: "请输入账号", color: UIColor.placeholderColor(), font:  UIFont.systemFont(ofSize: 17))
        
        bottomTexField.tintColor = UIColor.themeColor()
        
        lineView1.backgroundColor = UIColor.lineGray()
        
        lineView2.backgroundColor = UIColor.lineGray()
        
        loginBtn.addCornerAndBorder(borderColor: nil, borderWidth: 0.0, masksToBounds: true, cornerRadius: 5.0)
        
        regesterBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        
        findPasswordBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        
        
        bottomFirstLabel.textColor = UIColor.dakGray()
        
        bottomSendLabel.textColor = UIColor.themeColor()
        
        bottomThirdLabel.textColor = UIColor.dakGray()
        
        leftLineView.backgroundColor = UIColor.lineGray()
        
        rightLineView.backgroundColor = UIColor.lineGray()
   
        

    
    
    }



}
